package com.globalresources.dao.hibernate;

import java.util.List;

import com.globalresources.dao.inface.IMenuDao;
import com.globalresources.dao.inface.IMenuDaoHQL;
import com.globalresources.po.Menu;
import com.trg.dao.dao.standard.GenericDAOImpl;
import com.trg.search.Search;
import com.trg.dao.hibernate.BaseDAOImpl;
import java.util.List;
import com.trg.search.Search;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Jul 22, 2009 12:45:36 AM
 */
public class MenuDaoHibernate extends HibernateDaoSupport implements IMenuDao {
      public List<Menu> menus;      
      public Menu menu;

	/**
	 * query the menu
	*@author songduanlei@gmail.com
	*@param auth
	*@return
	 */
	public List<Menu> getMenus(String auth){
/*		Search search = new Search();
		search.addFilterEqual("authority", auth);
		search.addSort("sequnce", true);
		return search(search);*/
		
		return getHibernateTemplate().find("from Menu menu where menu.authority.authority='"+auth+"' order by menu.sequence desc");
	}
	

	
	
	/**
	 * save the menu
	*@author songduanlei@gmail.com
	*@param Object Menu
	*@return null
	 */
	public void saveMenus(Menu menu){	
		getHibernateTemplate().save(menu);
		
	}
	
	/**
	 * save or Update the menu
	*@author songduanlei@gmail.com
	*@param Object Menu
	*@return null
	 */
	public void saveOrUpdateMenus(Menu menu){	
		getHibernateTemplate().saveOrUpdate(menu);
	}
	
	/**
	 * get Menu By Id
	*@author songduanlei@gmail.com
	*@param Integer Id
	*
	*@return Menu
	 */
	public Menu getMenuById(Integer id){	
		return (Menu)(getHibernateTemplate().find("from Menu menu where menu.id='"+String.valueOf(id)+"'").get(0));
       }
	
	
	/**
	 * List Menu By Group
	 * 
	 * @author songduanlei@gmail.com
	 * @param null
	 * @return Menus
	 */
    @SuppressWarnings("unchecked")
	public List<Menu> listByGroup(){	
    //	select foo.id, avg(name), max(name) from Foo foo join foo.names name group by foo.id
    	menus=getHibernateTemplate().find("select menu  as sid from Menu menu group by menu.groups.groups");
    	return menus;
	}
	
	/**
	 * List Menu By Author
	 * 
	 * @author songduanlei@gmail.com
	 * @param null
	 * @return List Menus
	 */
    @SuppressWarnings("unchecked")
	public List<Menu> listByAuthor(){
    	menus=getHibernateTemplate().find("select menu from Menu menu group by menu.authority.id");
    	return menus;
	}
    
	/**
	 * List Menu By Author
	 * 
	 * @author songduanlei@gmail.com
	 * @param null
	 * @return List Menus
	 */
    @SuppressWarnings("unchecked")
	public List<Menu> findAll(){
    	menus=getHibernateTemplate().find("from Menu menu order by menu.id desc ");
    	return menus;
	}
    
    
	/**
	 * List Menu by sql
	 * 
	 * @author songduanlei@gmail.com
	 * @param null
	 * @return List Menus
	 */
    @SuppressWarnings("unchecked")
	public List<Menu> findBySql(String sql){
    	menus=getHibernateTemplate().find(sql);
    	return menus;
	}
	/**
	 * delete Menu By ID
	 * 
	 * @author songduanlei@gmail.com
	 * @param null
	 * @return List Menus
	 */
    @SuppressWarnings("unchecked")
	public boolean deleteMenuById(Integer id){
    	try{
    	/*menu=(Menu)(getHibernateTemplate().find("from Menu menu where menu.id'="+String.valueOf(id)+"'").get(0));
    	getHibernateTemplate().delete(menu);*/
    	List list=getHibernateTemplate().find("from Menu menu where menu.id='"+String.valueOf(id)+"'");
    	if(list.size()>0){
    	getHibernateTemplate().deleteAll(list);
    	}else{
    		return false;
    	}
    	return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		return false;
    	}
    	
    	}
}
